# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions
import csv
from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher
import time
import json
import requests
from typing import Any, Dict, List, Text, Optional
import datetime

data_list = []


# Title	Name	Review Date	Airline	Verified	Reviews	Type of Traveller	Month Flown	Route	Class	Seat Comfort	Staff Service	Food & Beverages	Inflight Entertainment	Value For Money	Overall Rating	Recommended	from				to	via


def load_csv():
    print("action_query_flight_with_class init - load_csv")
    with open('csv/min-arilines.csv') as f:
        read_data = csv.reader(f)
        is_first_row = True
        for row in read_data:
            if (is_first_row):
                print("skip first row")
                is_first_row = False
                continue
            item_from = row[17]
            item_to = row[21]
            print(f"item_from: {item_from}, item_to: {item_to}")
            data_list.append(row)

def item_sort_key(elem):
    #Overall Rating
    return elem[15]
def find_match_list(departure, destination, flight_class):
    match_item = []
    for item in data_list:
        item_from = item[17]
        item_to = item[21]
        item_class = item[9]
        if item_from.strip().lower() == departure.strip().lower() and item_to.strip().lower() == destination.strip().lower() and item_class.strip().lower() == flight_class.strip().lower():
            match_item.append(item)

    match_item.sort(key=item_sort_key)
    return match_item

load_csv()
class ActionQueryTrain(Action):

    def name(self) -> Text:
        return "action_query_flight_with_class"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        print("do action  action_query_flight_with_class")
        # extra slot
        departure = tracker.get_slot("departure")
        destination = tracker.get_slot("destination")
        flight_class = tracker.get_slot("flight_class")

        print(f"action extra,departure={departure},destination={destination},flight_class={flight_class}")

        match_item = find_match_list(departure, destination, flight_class)

        if match_item:
            msg = f"Below are the highest-rated flight options, please take a look. Would you like to book one of the flights?\n"
            for item in match_item:
                msg += f"name:{item[1]},rate:{item[15]},review:{item[2]}\n"
                msg += "\n"
            dispatcher.utter_message(msg)
        else:
            msg = f"I'm sorry, we don't have flight information from {departure} to {destination} of class {flight_class}. Do you have any other flight needs?"
            dispatcher.utter_message(msg)

        return []



if __name__ == '__main__':
    load_csv()
    match_list = find_match_list('Jakarta','Singapore','Economy Class')
    print(match_list)